//
//  PingjiaViewController.m
//  KIASupervisory
//
//  Created by apple on 15/7/30.
//  Copyright (c) 2015年 MyCompany. All rights reserved.
//
#import "PingjiaViewController.h"
#import "WeixViewController.h"
#import "MainViewController.h"
#import "SXDetailModel.h"
#import "SXDetailImgModel.h"
#import "SXHTTPManager.h"
#import "WeixListViewController.h"
#import "RatingBar.h"
@interface PingjiaViewController ()<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate>

@property (nonatomic, strong) NSMutableArray *ypList;
@property (nonatomic, strong) UIButton *back;
@property (nonatomic,strong) SXDetailModel *detailModel;
@property (nonatomic, strong) NetworkManager *networkManager;
@property (nonatomic, strong) MBProgressHUD *HUD;

@property (weak, nonatomic) IBOutlet UITextView *contentTv;
@property (nonatomic, strong) NSMutableArray *imageList;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (nonatomic, strong) UIAlertView *alertView;

@end

@implementation PingjiaViewController

@synthesize dataList = _dataList;
@synthesize bar = _bar;

- (void)viewDidLoad
{
    [self.navigationController setNavigationBarHidden:YES];
    self.networkManager = [[NetworkManager alloc] init];
    self.detailModel = [[SXDetailModel alloc] init];
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.delegate = self;
    [super viewDidLoad];
     bar  = [[RatingBar alloc] initWithFrame:CGRectMake(20, 80, 180, 30)];
    [self.view addSubview:bar];
    
}

- (void) textViewDidChange:(UITextView *)textView{
    if ([textView.text length] == 0) {
        [_label setHidden:NO];
    }else{
        [_label setHidden:YES];
    }
}

- (IBAction)backToPre:(id)sender {
//    WeixListViewController *problemVC = (WeixListViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"WeixListViewController"];
//    CATransition* transition = [CATransition animation];
//    transition.type = kCATransitionPush;//可更改为其他方式
//    transition.subtype = kCATransitionFromRight;//可更改为其他方式
//    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
//    [self.navigationController pushViewController:problemVC animated:NO];
[self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)pingjia:(id)sender {
    NSInteger starNumber = [bar starNumber];
    if(starNumber == 0){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"请先填写评级星级提交"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    }
   else if ([self.contentTv hasText]) {
       NSString *wtms = self.contentTv.text;
       NSDictionary *data = self.wxInfo;
       NSString *id = [data objectForKeyOrNil:@"id"];
       NSString *nums = [NSString stringWithFormat: @"%ld", (long)starNumber];
        NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                                  id,@"weixiu_id",
                               nums,@"pjxj",
                               wtms,@"pj_note",
                               nil];
           [self.networkManager requestWithMethod:weixiuPj parameter:param delegate:self];
           [self.HUD show:YES];
   } else {
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                       message:@"请先输入评价内容再提交"
                                                      delegate:nil
                                             cancelButtonTitle:@"确定"
                                             otherButtonTitles:nil];
       [alert show];

   }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];

}

//根据已有数据刷新界面
- (void)updateViewWithData {
 
}

-(void) getContent:(NSString*)id {
    //NSString *wenzhang_id = @"5";
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           id,@"tongzhi_id", nil];
    [self.networkManager requestWithMethod:tongzhiDetail parameter:param delegate:self];
    [self.HUD show:YES];
}


- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFinishedWithResult:(id)data {
    [self.HUD hide:YES];
    
    if (methodId == weixiuPj) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * result = [data objectForKeyOrNil:@"result"];
            if([result isEqualToString:@"000"]){
                NSString * resultinfo = [data objectForKeyOrNil:@"resultinfo"];
                
                NSString *unicodeStr = [resultinfo stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                [self showAlertViewWithMessage:unicodeStr];
                WeixListViewController *problemVC = (WeixListViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"WeixListViewController"];
                UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:problemVC];
                DDMenuController *menuVC = [[DDMenuController alloc] initWithRootViewController:navController];
 
                [self.navigationController pushViewController:menuVC animated:YES];

                
            }
        }
        
    }
    
}

- (void)showAlertViewWithMessage:(NSString *)msg {
    if ([msg length] > 0) {
        _alertView = [[UIAlertView alloc] initWithTitle:@""
                                                message:msg
                                               delegate:self
                                      cancelButtonTitle:@"确定"
                                      otherButtonTitles:nil];
        _alertView.tag = 10001;
        [NSTimer scheduledTimerWithTimeInterval:0.9f
                                         target:self
                                       selector:@selector(performDismissAlert)
                                       userInfo:nil
                                        repeats:NO];
        [_alertView show];
    }
}

- (void)performDismissAlert {
    if (_alertView) {
        [_alertView dismissWithClickedButtonIndex:0 animated:NO];
        _alertView = nil;
    }
}


- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFailedWithError:(NSError *)error {
    [self.HUD hide:YES];
    NSDictionary *errorInfo = error.userInfo;
    NSString *msg = [errorInfo objectForKeyOrNil:@"errorMsg"];
    
    NSInteger code = error.code;
    if (code != 1) {
        if ([msg length] == 0) {
            msg = @"历史未完成问题获取失败，请稍后重试";
        }
    }
    
    if ([msg length] > 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:msg
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    }
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_showData count]>0?[_showData count]:0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             TableSampleIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:TableSampleIdentifier];
    }
    if (_showData != nil && _showData.count >0) {
        NSUInteger row = [indexPath row];
        cell.textLabel.text = [_showData objectAtIndex:row];
    }
    return cell;
}



@end